<?php
namespace Admin\Controller;

class BaseController extends \Api\Core\Web {


    public $admin = null;
    public $term = null;
    public $domain = 'http://www.e8net.cn';

    public function __construct() {
        parent::__construct();
        $this->initialise();
    }


    protected function initialise() {
        $this->checkLogin();
        $this->getMenu();
        $this->admin = session ('admin');
        $this->term = session ('term');
    }

    /**
     * 获取后台菜单列表
     */
    protected function getMenu() {
        $Menu = D ('Menu');
        $menu = $Menu::$_menu;

        // 处理菜单
        foreach ($menu as $key=>$val) {
            if ($val['controller'] == CONTROLLER_NAME) {
                $menu[$key]['active'] = 'active';
                break;
            }
            if (!empty($menu[$key]['child'])) {
                foreach ($menu[$key]['child'] as $k=>$v) {
                    if ($v['controller'] == CONTROLLER_NAME) {
                        $menu[$key]['active'] = 'active';
                        $menu[$key]['child'][$k]['active'] = 'active';
                        break;
                    }
                }
            }
        }
        //p($menu);
        $this->assign('menu',$menu);
    }


    // 验证用户是否登录
    protected function checkLogin() {
        if (is_null ( session ( 'admin' ) )) {
            $this->redirect('Login/index');
            die;
        }
    }


    // 权限验证
    protected function check_perm() {
        $Perm = D("Perm");
        $admin = session("admin");
        if ($admin['super'] == 1)
            return;
        //生成权限数组
        $permArr = explode(",",$admin['perm']);
        $controller = array();
        foreach ($permArr as $key => $value) {
            $controller[] = $Perm->where("id = $value")->getField("controller");
        }
        $nowcontroller = CONTROLLER_NAME;
        // 验证权限
        if ($admin['super'] == 0 and ! in_array ( $nowcontroller, $controller)) {
            $this->error ( "您没有权限操作当前模块" );
        }
    }
}